package com.aixin.csopservice.service.impl;

import com.aixin.common.core.domain.model.LoginBody;
import com.aixin.csopservice.mapper.SupplierMapper;
import com.aixin.csopservice.model.ReturnCustomer;
import com.aixin.csopservice.model.ReturnSupplier;
import com.aixin.csopservice.model.ReturnSupplierService;
import com.aixin.csopservice.service.SupplierService;
import com.aixin.csopservice.utils.RSA2Util;
import com.aixin.csopservice.utils.Util;
import com.aixin.csopsupplier.domain.SupplierDef;
import com.aixin.csopsupplier.model.SupplierDefModel;
import com.aixin.csopsupplier.service.ServiceDefService;
import com.aixin.csopsupplier.service.SupplierDefService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class SupplierServiceImpl implements SupplierService {


    @Autowired
    SupplierDefService supplierDefService;

    @Autowired
    ServiceDefService serviceDefService;

    @Autowired
    SupplierMapper supplierMapper;

    @Override
    public ReturnSupplier getSupplier(SupplierDefModel supplierDefModel) {
        ReturnSupplier returnSupplier=new ReturnSupplier();
        LoginBody loginBody=new LoginBody();
        loginBody.setUsername(supplierDefModel.getUserName());
        loginBody.setPassword(supplierDefModel.getPassword());
      SupplierDef supplierDef = supplierDefService.loginSupplier(loginBody);
        if(supplierDef!=null && !"".equals(supplierDef.getSupplierID())){
            returnSupplier.setErrorMsg("鉴权成功");
            returnSupplier.setRequestTime(Util.getNowDate());
            returnSupplier.setReturnResultFlag("SUCCESS");
            try {
                returnSupplier.setSupplierID(RSA2Util.encrypt(supplierDef.getSupplierID()));

            }catch (Exception e){
                returnSupplier.setErrorMsg("系统加密失败,请联系我们!");
                return returnSupplier;
            }
            return returnSupplier;
        }
        returnSupplier.setErrorMsg("鉴权失败,无此供应商");
        returnSupplier.setRequestTime(Util.getNowDate());
        returnSupplier.setReturnResultFlag("FALSE");
        return returnSupplier;

    }

    @Override
    public ReturnSupplierService getSupplierService(String supplierID) {
        ReturnSupplierService returnSupplierService=new ReturnSupplierService();
        List<Map<String,String>> list= supplierMapper.getService(supplierID);
        returnSupplierService.setReturnResultFlag("SUCCESS");
        returnSupplierService.setRequestTime(Util.getNowDate());
        returnSupplierService.setServiceList(list);
        return returnSupplierService;
    }

    @Override
    public ReturnCustomer getCustomer(String supplierID) {
        ReturnCustomer returnCustomer= new ReturnCustomer();
        List<Map<String,String>> list= supplierMapper.getCustomer(supplierID);
        returnCustomer.setReturnResultFlag("SUCCESS");
        returnCustomer.setRequestTime(Util.getNowDate());
        returnCustomer.setCustomerList(list);
        return returnCustomer;
    }


}
